package cn.lkk.samples.lang.fundamental.Arrays;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 一个彩票有游戏，用于模拟数组的基本使用方法。
 * 从1-n个数中随机选择选择出k个，保证无重复。
 */
public class LotteryDrawing {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println("How many numbers you need to draw");
        int k = in.nextInt();

        System.out.println("What is the highest number you draw?");
        int n = in.nextInt();

        //定义一个数组并初始化为1到n
        int[] numbers = new int[n];
        for (int i = 0; i < numbers.length; ++i)
            numbers[i] = i + 1;

        int[] result = new int[k];
        for (int i = 0; i < k; ++i) {
            //随机选择一个数放到result数组中
            int index = (int) (Math.random() * n);
            int value = numbers[index];
            result[i] = value;

            //解决无重复问题
            numbers[index] = numbers[n - 1];
            n--;
        }

        Arrays.sort(result);
        System.out.println("Bet the following combination. It'll make you rich!");
        System.out.println(Arrays.toString(result));

    }
}
